package com.topboya.linker.ui.adapter;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.topboya.linker.R;
import com.topboya.linker.model.response.HomeListResponse;
import com.topboya.linker.ui.activity.xMainActivity;
import com.topboya.linker.ui.presenter.HomePresenter;
import com.topboya.linker.utils.LogUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class HomeListAdapter extends BaseQuickAdapter<HomeListResponse.TaskList, BaseViewHolder> {

    private static final String TAG = "HomeListAdapter:";
    private xMainActivity mainActivity;
    private List<HomeListResponse.TaskList> taskLists;
    private HomePresenter homePresenter;

    public HomeListAdapter(xMainActivity mainActivity, HomePresenter homePresenter, List<HomeListResponse.TaskList> taskLists) {

        super(R.layout.home_task_item, taskLists);
        this.mainActivity = mainActivity;
        this.homePresenter = homePresenter;
        this.taskLists = taskLists;
    }

    @Override
    protected void convert(BaseViewHolder viewHolder, HomeListResponse.TaskList taskList) {
        LogUtils.v(TAG+"taskList:"+taskList.toString());
       viewHolder.setText(R.id.tv_timekey, getTime(taskList.getStartTime()));
        viewHolder.setText(R.id.tv_orgname, taskList.getOrgName());
        viewHolder.setText(R.id.tv_cusname,  taskList.getCusName());
        if (null != taskList.getStatus()) {
            int status = Integer.parseInt(taskList.getStatus());
            String[] statusAaary = mContext.getResources().getStringArray(R.array.signin_status);
            if (status==1||status ==2||status ==4){
                LogUtils.v(TAG + "tv_status:"+status);
                viewHolder.setGone(R.id.tv_status,true);
                viewHolder.setText(R.id.tv_status,statusAaary[status]);
                viewHolder.getView(R.id.tv_status).setBackgroundResource(getSignStatusBg(status));
//             暂时三种状态显示1 签入 2 反馈（已签入可签出） 4 正常签出 其他状态不显示
                if (status==4){
                    viewHolder.setGone(R.id.tv_endtiem,true);
                    viewHolder.setText(R.id.tv_endtiem,getTime(taskList.getEndTime()));
//                表示正常签出，就显示签出时间，否则就不显示
                }else {
                    viewHolder.setGone(R.id.tv_endtiem, false);
                }
            }else {
                viewHolder.setGone(R.id.ll_sign_status,false);
            }
        }

        viewHolder.addOnClickListener(R.id.tv_status);
    }


    private String getTime(String time) {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            try {
                Date date = sdf.parse(time);
           SimpleDateFormat format = new SimpleDateFormat("HH:mm");

               String data = format.format(date);
               return data ;
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

           return "";
        }


//0:不能签入
//1:可签入
//2:已签入可迁出
//3:已签入超过三公里
//4：正常签出
//5：非正常签出
//6：非正常签出
        public int getSignStatusBg(int status){
            int  bgid;
            switch (status){
                case 1:
                    bgid =  R.drawable.signin_oval_bg;
                    break;
                case 2:
                    bgid =  R.drawable.feedback_oval_bg;
                    break;
                case 4:
                    bgid =  R.drawable.complete_oval_bg;
                    break;
                 default:
                     bgid =  R.drawable.complete_oval_bg;
                     break;
            }
           return bgid;
        }

}
